//
//  ZXJPasswordLoginViewController.m
//  academic_records
//
//  Created by zhang bozhao on 2020/8/5.
//  Copyright © 2020 zhang bozhao. All rights reserved.
//

#import "ZXJPasswordLoginViewController.h"
#import "ZXJVerityCodeLoginViewController.h"
#import "ZXJVerityCodeLoginViewController.h"
#import "LoginRequest.h"
#import "ZXJHomePageViewController.h"
#import "AcademicTabbarViewController.h"

@interface ZXJPasswordLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;

@end

@implementation ZXJPasswordLoginViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.phoneView.layer.cornerRadius = 22.0f;
    self.passwordView.layer.cornerRadius = 22.0f;
    self.loginBtn.layer.cornerRadius = 22;
    self.phoneTF.textColor = HexRGB(0x333333);
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    self.passwordTF.secureTextEntry = YES;
    self.phoneTF.delegate = self;
    self.passwordTF.delegate = self;
}
- (IBAction)loginAction:(id)sender {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:_phoneTF.text forKey:@"Phone"];
    [params setValue:[NSString stringWithFormat:@"%@",self.passwordTF.text] forKey:@"Passwd"];
    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"IPhoneUUID"] forKey:@"sessionID"];
    [LoginRequest do_lgoinByPassword:params :^(id  _Nullable data, ResultCode resultCode, NSError * _Nullable Error) {
        NSDictionary *dict = data;
        if (resultCode == SucceedCode) {
            AcademicTabbarViewController *minenav = [[AcademicTabbarViewController alloc]init];
            APP_DELEGATE.window.rootViewController=minenav;
            NSString *userid = [NSString stringWithFormat:@"%@",[dict objectForKey:@"uGuid"]];
            [[NSUserDefaults standardUserDefaults]setObject:userid forKey:@"USERID"];
            [[NSUserDefaults standardUserDefaults]setObject:@(YES) forKey:@"IS_LOGIN"];
            [[NSUserDefaults standardUserDefaults]setObject:@"未绑定" forKey:@"BindingWeChat"];
            [[NSUserDefaults standardUserDefaults]setObject:[NSMutableArray array] forKey:HistoryArr];
            [[NSUserDefaults standardUserDefaults]setObject:self.phoneTF.text forKey:@"PHONENUM"];
        }
    }];
}
- (IBAction)changePasswordSecurity:(id)sender {
    UIButton *btn = sender;
    btn.selected =!btn.selected;
    if (btn.selected == YES) {
        [btn setImage:[UIImage imageNamed:@"yanshi"] forState:(UIControlStateNormal)];
        self.passwordTF.secureTextEntry = NO;
    }else{
        [btn setImage:[UIImage imageNamed:@"yingcang"] forState:(UIControlStateNormal)];
         self.passwordTF.secureTextEntry = YES;
    }
}
- (IBAction)verityCodeAction:(id)sender {
    ZXJVerityCodeLoginViewController *verityVC = [[ZXJVerityCodeLoginViewController alloc]init];
    verityVC.type = 2;
    [self.navigationController pushViewController:verityVC animated:YES];
}
- (IBAction)forgetPassWordAction:(id)sender {
//    NSNumber * is_login =  [[NSUserDefaults standardUserDefaults]objectForKey:@"IS_LOGIN"];
//    if (is_login.boolValue == NO) {
//        [self.navigationController popViewControllerAnimated:YES];
//    }else{
        ZXJVerityCodeLoginViewController *verityVC = [[ZXJVerityCodeLoginViewController alloc]init];
        verityVC.type = 3;
        [self.navigationController pushViewController:verityVC animated:YES];
//    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([self validateMobile:self.phoneTF.text] && self.passwordTF.text.length != 0) {
        [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"denglu-2"] forState:(UIControlStateNormal)];
        self.loginBtn.userInteractionEnabled = YES;
    }else{
        [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"denglu-1"] forState:(UIControlStateNormal)];
        self.loginBtn.userInteractionEnabled = NO;
    }
}
#pragma mark 判断是否是手机号
-(BOOL)validateMobile:(NSString *)mobile{
    NSString * MOBILE = @"^1(3[0-9]|4[579]|5[0-35-9]|6[6]|7[0-35-9]|8[0-9]|9[89])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    if (([regextestmobile evaluateWithObject:mobile] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
